﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication
{
    public partial class QueueTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSendMessage_Click(object sender, EventArgs e)
        {
            System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue(".\\Private$\\PSLogs");
            LogInfo logInfo = new LogInfo()
            {
                createdate=DateTime.Now,
                message=txtSendMessage.Text
            };
            // Create message
            System.Messaging.Message message = new System.Messaging.Message();
            message.Body = logInfo;
            message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(LogInfo) });

            queue.Send(logInfo); 
        }

        protected void btnReceiveMessage_Click(object sender, EventArgs e)
        {
            System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue(".\\Private$\\PSLogs");
            //LogInfo logInfo = (LogInfo)queue.Receive();
            System.Messaging.Message message = queue.Receive();
            message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(LogInfo) });
            LogInfo logInfo = (LogInfo)message.Body;
            txtReceiveMessage.Text = logInfo.message;
        }

        public class LogInfo
        {
            public DateTime createdate { get; set; }
            public string message { get; set; }
        }

    }
}